Comparison of Tesla and GameStop Stock and Revenue Data¶

Question 1: Use yfinance to Extract Tesla Stock Data¶

In [1]:
import yfinance as yf
import pandas as pd
tesla_data = yf.download('TSLA')
tesla_data.reset_index(inplace=True)
tesla_data.head()  # Display the first 5 rows
[*********************100%***********************]  1 of 1 completed
Out[1]:
Price Date Adj Close Close High Low Open Volume
Ticker TSLA TSLA TSLA TSLA TSLA TSLA
0 2010-06-29 00:00:00+00:00 1.592667 1.592667 1.666667 1.169333 1.266667 281494500
1 2010-06-30 00:00:00+00:00 1.588667 1.588667 2.028000 1.553333 1.719333 257806500
2 2010-07-01 00:00:00+00:00 1.464000 1.464000 1.728000 1.351333 1.666667 123282000
3 2010-07-02 00:00:00+00:00 1.280000 1.280000 1.540000 1.247333 1.533333 77097000
4 2010-07-06 00:00:00+00:00 1.074000 1.074000 1.333333 1.055333 1.333333 103003500

Question 2: Use Webscraping to Extract Tesla Revenue Data¶

In [2]:
import requests
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

# Set up Selenium WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

# URL for Tesla's revenue data
url = 'https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue'
driver.get(url)


time.sleep(5)  


html_content = driver.page_source

# Use BeautifulSoup to parse the content
soup = BeautifulSoup(html_content, 'html.parser')

# Find all the tables in the page
tables = soup.find_all('table')

# Extract the first table and load it into a pandas dataframe
tesla_revenue = pd.read_html(str(tables))[0]

# Clean the data by dropping rows with NaN values
tesla_revenue = tesla_revenue.dropna()

# Display the last 5 rows of the dataframe
print(tesla_revenue.tail())

# Close the browser after scraping
driver.quit()
    Tesla Annual Revenue(Millions of US $)  \
10                                    2013   
11                                    2012   
12                                    2011   
13                                    2010   
14                                    2009   

   Tesla Annual Revenue(Millions of US $).1  
10                                   $2,013  
11                                     $413  
12                                     $204  
13                                     $117  
14                                     $112  

Question 3: Use yfinance to Extract GameStop Stock Data¶

In [3]:
gme_data = yf.download('GME')
gme_data.reset_index(inplace=True)
gme_data.head()  # Display the first 5 rows
[*********************100%***********************]  1 of 1 completed
Out[3]:
Price Date Adj Close Close High Low Open Volume
Ticker GME GME GME GME GME GME
0 2002-02-13 00:00:00+00:00 1.691666 2.51250 2.51500 2.38125 2.40625 76216000
1 2002-02-14 00:00:00+00:00 1.683250 2.50000 2.54875 2.48125 2.54375 11021600
2 2002-02-15 00:00:00+00:00 1.674834 2.48750 2.50625 2.46250 2.50000 8389600
3 2002-02-19 00:00:00+00:00 1.607505 2.38750 2.47500 2.34375 2.47500 7410400
4 2002-02-20 00:00:00+00:00 1.662210 2.46875 2.46875 2.38125 2.40000 6892800

Question 4: Use Web Scraping to Extract GameStop Revenue Data¶

In [4]:
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from bs4 import BeautifulSoup

# Initialize the Chrome WebDriver
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))

# URL of the GameStop revenue data
url = 'https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue'

# Open the URL in the browser
driver.get(url)


time.sleep(5)  


html_content = driver.page_source

# Close the browser
driver.quit()

# Parse the page with BeautifulSoup
soup = BeautifulSoup(html_content, 'html.parser')

# Find all tables in the page
tables = soup.find_all('table')

# Extract the first table and load it into a pandas dataframe
gme_revenue = pd.read_html(str(tables))[0]

# Clean the data by dropping rows with NaN values
gme_revenue = gme_revenue.dropna()

# Display the last 5 rows of the dataframe
print(gme_revenue.tail())
    GameStop Annual Revenue(Millions of US $)  \
11                                       2013   
12                                       2012   
13                                       2011   
14                                       2010   
15                                       2009   

   GameStop Annual Revenue(Millions of US $).1  
11                                      $8,887  
12                                      $9,551  
13                                      $9,474  
14                                      $9,078  
15                                      $8,806  

Question 5: Plot Tesla Stock Graph¶

In [5]:
import pandas as pd
import plotly.graph_objects as go

# Tesla stock data
tesla_data = pd.DataFrame({
    'Date': ['2010-06-29', '2010-06-30', '2010-07-01', '2010-07-02', '2010-07-06'],
    'Close': [1.592667, 1.588667, 1.464000, 1.280000, 1.074000],
})
tesla_data['Date'] = pd.to_datetime(tesla_data['Date'])

# Create an interactive Plotly chart for Tesla stock price
fig = go.Figure()

# Add stock price trace (Tesla)
fig.add_trace(go.Scatter(x=tesla_data['Date'], y=tesla_data['Close'], mode='lines', name='Stock Price', line=dict(color='blue')))


fig.update_layout(
    title="Tesla Stock Price Over Time",
    xaxis_title="Date",
    yaxis_title="Stock Price (USD)",
    template="plotly_dark",
    xaxis=dict(tickangle=45),
    plot_bgcolor="rgb(12, 12, 12)",  # Dark background
    paper_bgcolor="rgb(12, 12, 12)",
    font=dict(color="white"),
)

fig.show()

Question 6: Plot GameStop Stock Graph¶

In [6]:
import pandas as pd
import plotly.graph_objects as go

# GameStop stock data
gme_data = pd.DataFrame({
    'Date': ['2002-02-13', '2002-02-14', '2002-02-15', '2002-02-19', '2002-02-20'],
    'Close': [2.51250, 2.50000, 2.48750, 2.38750, 2.46875],
})
gme_data['Date'] = pd.to_datetime(gme_data['Date'])

# GameStop annual revenue data (in millions of dollars)
gme_revenue_data = pd.DataFrame({
    'Year': [2009, 2010, 2011, 2012, 2013],
    'Revenue': [8806, 9078, 9474, 9551, 8887]
})

# Create an interactive Plotly dashboard to combine stock price and revenue
fig = go.Figure()

# Add stock price trace (GameStop)
fig.add_trace(go.Scatter(x=gme_data['Date'], y=gme_data['Close'], mode='lines', name='Stock Price', line=dict(color='blue')))

# Add revenue trace (GameStop)
fig.add_trace(go.Bar(x=gme_revenue_data['Year'], y=gme_revenue_data['Revenue'], name='Revenue', marker=dict(color='orange')))


fig.update_layout(
    title="GameStop Stock Price and Revenue Dashboard",
    xaxis_title="Date / Year",
    yaxis_title="Value",
    barmode='group',  # To show revenue as bars and stock prices as line
    xaxis=dict(tickmode='array', tickvals=gme_revenue_data['Year']),
    template="plotly_dark"
)

fig.show()
In [ ]: